﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Class_KetNoi;

namespace GiaoDien
{
    public partial class uc_KyLuat : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_KyLuat()
        {
            InitializeComponent();
        }

        KetNoi_KyLuat kn = new KetNoi_KyLuat();
        public void LoadDuLieu()
        {
            string sql = "select * from tbl_KiLuat";
            gridControl1.DataSource = kn.taobang(sql);
        }
        public void Nut()
        {
            btnThem.Enabled = false;
            btnSua.Enabled = false;
            btnXoa.Enabled = false;

            btnLuu.Enabled = true;
            btnHuy.Enabled = true;
        }
        private void uc_KyLuat_Load(object sender, EventArgs e)
        {
            kn.connect();
            lbMaKyLuat.Hide();
            txtMaKyLuat.Hide();
            LoadDuLieu();

        }
        int flag;
        private void btnThem_Click(object sender, EventArgs e)
        {
            flag = 0;
            Nut();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            flag = 1;
            Nut();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            kn.xoa_KyLuat(txtMaKyLuat.Text );
            LoadDuLieu();
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                kn.them_KyLuat(txtTenKyLuat.Text ,txtMaNV.Text ,date_NgayKyLuat.Text,txtHinhThuc.Text,txtNguyenNhan.Text );
            }
            if (flag == 1)
            {
                kn.sua_KyLuat(txtMaKyLuat.Text, txtTenKyLuat.Text, txtMaNV.Text, date_NgayKyLuat.Text, txtHinhThuc.Text, txtNguyenNhan.Text);
            }
            LoadDuLieu();
            btnThem.Enabled =true;
            btnSua.Enabled = true;
            btnXoa.Enabled = true;

            btnLuu.Enabled = false;
            btnHuy.Enabled = false;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            txtMaKyLuat.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"MaKL").ToString();
            txtTenKyLuat.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"TenKL").ToString();
            txtMaNV.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"MaNV").ToString();
            date_NgayKyLuat.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"NgayKL").ToString();
            txtHinhThuc.Text=gridView1.GetRowCellValue(gridView1.FocusedRowHandle,"HinhThuc").ToString();
            txtNguyenNhan.Text = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, "NguyenNhan").ToString();
        }
    }
}
